package com.dfbz.demo;

import java.util.concurrent.*;

/**
 * @author lscl
 * @version 1.0
 * @intro:
 */
public class Demo03_线程池执行Callable任务 {
    public static void main(String[] args) throws Exception {
        ExecutorService executorService = Executors.newFixedThreadPool(2);

        MyCallable callableTask = new MyCallable();

        // 执行Callable任务
        Future<String> future = executorService.submit(callableTask);

        // 这个方法会阻塞的,因为要等Callable任务执行完毕才能拿到结果集(call方法的返回值)
        String result = future.get();

        System.out.println("结果集为: " + result);
    }
}


class MyCallable implements Callable<String> {

    @Override
    public String call() throws Exception {

        Thread.sleep(3000);

        System.out.println("callable执行完毕...");
        return "ok";
    }
}